﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RndSamples
{
    class Processor
    {
        string delimiters = " ,.;!?\n\r\t";
        int numWordSelect = 30000;
        public void ProcessFile(FileInfo fi) 
        {
            Console.WriteLine("Processing " + fi.Name);
            TextReader tr_in = new StreamReader(fi.FullName);

            string strSource;
            strSource = tr_in.ReadToEnd();
            tr_in.Close();
            //getting total number of words
            bool isWordCounted = false;
            int wordCount = 0;
            //string tempWord="";
            for (int i = 0; i < strSource.Length; i++)
            {
                //Console.WriteLine("Triing " + strSource[i].ToString() + "=" +((int)strSource[i]).ToString());
                if (delimiters.Contains(strSource[i]) )
                {
                    isWordCounted = false;
                }
                else
                {
                    if (!isWordCounted)
                    {
                        wordCount++;
                        isWordCounted = true;
                        //Console.WriteLine("First symbol of new word = "+((int)strSource[i]).ToString());

                    }
                }

            }
            Console.WriteLine("Words count = " + wordCount.ToString());
            if (wordCount < numWordSelect)
            {
                Console.WriteLine("Not enough words to select " + numWordSelect.ToString());
                return ;
            }
            Random w_rnd = new Random(DateTime.Now.Millisecond);
        
            int startPosition =w_rnd.Next(0, wordCount-numWordSelect); //width
            Console.WriteLine("Selecting "+ numWordSelect.ToString()+" words starting at word " + startPosition.ToString());
            //writing words
            TextWriter tw_out = new StreamWriter("./out/" + fi.Name);
            //bool isStartPassed = false;
            //bool isFinishPassed = false;
            int currentWordNum=0;
            for (int i = 0; i < strSource.Length; i++)
            {
                //Console.WriteLine("Triing " + strSource[i].ToString() + "=" +((int)strSource[i]).ToString());

                if (delimiters.Contains(strSource[i]))
                {
                    isWordCounted = false;
                }
                else
                {
                    if (!isWordCounted)
                    {
                        currentWordNum++;
                        isWordCounted = true;
                        //Console.WriteLine("First symbol of new word = "+((int)strSource[i]).ToString());

                    }
                }
                if (currentWordNum >= startPosition)
                    tw_out.Write(strSource[i]);

                if (currentWordNum >= startPosition + numWordSelect)
                    break;

            }
            tw_out.Close();

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, random samples");
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };

            Processor processor = new Processor();
            foreach (FileInfo fi in Files)
            {
                processor.ProcessFile(fi);
            }

            



        }
    }
}
